import { MapCreater } from "./MapCreater";
import { Player } from "./Player"
import { arr_hallPlayer, map_room, numOfHallPlayers, sendObj } from "./WebSocket";

export class Room{

    constructor(id:number,type:number){
        this.id = id;
        this.type = type;
    }

    id:number                           //房间ID,唯一标识
    state:RoomState = RoomState.OPEN    //状态 
    type:number                         //房间类型，2人局还是3人局 
    arr_players:Array<Player> = new Array()            //玩家
    
    //观战玩家
    //操作记录

    /**
     * 放进一个玩家
     */
    addPlayer(player:Player){
        const set = new Set(this.arr_players).add(player)
        this.arr_players = Array.from(set)
        player.roomId = this.id;
        if(this.arr_players.length == this.type){
            this.prepare()
        }
    }

    /**
     * 人数足够，准备开始，先将地图信息传过去
     */
    prepare(){
        this.state = RoomState.PLAYING
        this.arr_players.forEach((player,index)=>{
            sendObj(player.connection,{other:"matching_success",seat:index})
            player.success()
        })
        const map = new MapCreater().createMap();
        this.broadcast({method:"build",map,type:this.type})
    }

    /**
     * 减少玩家
     * 取消匹配
     */
    reducePlayer(player:Player){
        const set = new Set(this.arr_players);
        set.delete(player);
        this.arr_players = Array.from(set);
        if(this.arr_players.length==0){
            this.dissolution();
        }
    }

    /**
     * 解散
     */
    dissolution(){
        if(this.state == RoomState.CLOSE){
            return;
        }
        console.log("房间解散： ",this.id);
        this.state = RoomState.CLOSE
        this.arr_players.forEach(plyer=>{
            sendObj(plyer.connection,{method:"dissolution"});
            plyer.roomId = -1;
        })
        this.arr_players = null;
        map_room.delete(this.id)
    }


    /**
     * 房间内player收到消息
     */
    num = 0;        //当前ready人数
    turn_player:Player; //当前回合玩家  
    onMessage(obj:any){
        switch(obj.method){
            case "ready": 
                this.num++;
                if(this.num==this.type){
                    console.log("开始对局，房间号："+this.id+"  房间人数："+this.type);
                    this.broadcast({method:"start"})
                    this.broadcast({method:"turn",id:this.arr_players[0].id})
                }
                break;
            case "turn":
                let nextId = 0;
                for(let i=0;i<this.arr_players.length;i++){
                    if(obj.id==this.arr_players[i].id){
                        if(i<this.arr_players.length-1){
                            nextId = i+1;
                        }
                        break;
                    }
                }
                this.turn_player =this.arr_players[nextId]
                this.broadcast({method:"turn",id:this.turn_player.id}) 
                break;
            case "over":
                this.dissolution();
                break;
            default :this.broadcast(obj) 
        }
    }

    /**
     * 房间内广播
     */
    broadcast(obj:Object){
        this.arr_players.forEach(player=>{
            sendObj(player.connection,obj)
        })
    }

    /**
     * 玩家掉线
     */
    dropLine(player:Player){
        //先让这个掉线的回合结束
        if(this.turn_player == player){
            this.onMessage({method:"turn",id:player.id})
        }
        //整理player数组
        for(let i=0;i<this.type;i++){
            if(this.arr_players[i]===player){
                this.arr_players.splice(i,1);
                break;
            }
        }
        //通知其他player掉线
        this.arr_players.forEach(player2=>{
            sendObj(player2.connection,{method:"dropLine",id:player.id})
        })
        //解散房间
        if(this.arr_players.length==0){
            this.dissolution();
        }
    }
}

export enum RoomState{
    OPEN,PLAYING,CLOSE
}